$(function () {
    var layer = layui.layer
    var id = window.parent.id
    var form = layui.form
    console.log(id);

    form.verify({
        nickname: function (value) {
            if (value.length > 6) {
                return '昵称长度必须是1-6个字符'
            }
        }
    })
    $.ajax({
        type: 'get',
        url: 'http://localhost:8888/admin/users/' + id,
        headers: {
            Authorization: localStorage.getItem('token')
        },
        success: function (res) {
            if (res.status !== 0) {
                return layer.msg('信息获取失败')
            }
            form.val('editForm', res.data)
        }
    })

    $('.layui-form').on('submit', function (e) {
        //阻止默认提交行为
        e.preventDefault()

        //获取数据
        var data = $(this).serialize()
        console.log(data);
        //发送请求
        $.ajax({
            type: 'put',
            url: 'http://localhost:8888/admin/upuser',
            data: data,
            headers: {
                Authorization: localStorage.getItem('token')
            },
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('修改用户信息失败')
                }
                layer.msg('修改用户信息成功', function () {
                    location.href = '/user/user.html'
                })
            }
        })
    })

})